import { NextRequest, NextResponse } from "next/server"
import fs from "fs"
import path from "path"

// 提供PDF文件的API路由
export async function GET(request: NextRequest) {
  try {
    // PDF文件路径
    const pdfPath = path.join(process.cwd(), "app", "about", "mypdf10.pdf")
    console.log("PDF文件路径:", pdfPath)
    
    // 检查文件是否存在
    if (!fs.existsSync(pdfPath)) {
      console.error("PDF文件不存在:", pdfPath)
      return new NextResponse("PDF文件不存在", { status: 404 })
    }
    
    // 获取文件状态信息
    const stats = fs.statSync(pdfPath)
    console.log("PDF文件大小:", stats.size, "字节")
    
    if (stats.size === 0) {
      console.error("PDF文件大小为0")
      return new NextResponse("PDF文件为空", { status: 500 })
    }
    
    // 读取PDF文件
    const pdfBuffer = fs.readFileSync(pdfPath)
    console.log("成功读取PDF文件，大小:", pdfBuffer.length, "字节")
    
    // 返回PDF文件
    return new NextResponse(pdfBuffer, {
      headers: {
        "Content-Type": "application/pdf",
        "Content-Disposition": "inline; filename=mypdf10.pdf",
        "Content-Length": String(pdfBuffer.length),
        "Cache-Control": "no-cache"
      }
    })
  } catch (error) {
    console.error("Error serving PDF file:", error)
    return new NextResponse("获取PDF文件失败: " + (error as Error).message, { status: 500 })
  }
}